package com.example.demo.dao;

import com.example.demo.entity.TestVo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TestDao {
    public static final String JDBC_URL = "jdbc:mysql://localhost:3306/chap04?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true";
    public static final String JDBC_USER = "root";
    public static final String JDBC_PASSWORD = "20040219";

    public TestVo findById(Integer id) throws Exception {

        TestVo testVo=new TestVo();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
            ps = con.prepareStatement("select  * from t_user where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {

                testVo.setLogin_name(rs.getString("login_name"));
                testVo.setPwd(rs.getString("pwd"));

            }
        } catch (Exception e) {
            throw new Exception("DAO异常:" + e.getMessage());
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return testVo;
    }
}
